package com.znsb.msfq.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.znsb.msfq.BaseActivity;
import com.znsb.msfq.R;
import com.znsb.msfq.app.ZnsbApp;
import com.znsb.msfq.utils.GetIsparityTime;
import com.znsb.msfq.utils.HttpUtils;
import com.znsb.msfq.utils.ImageLoader;
import com.znsb.msfq.utils.JsonResult;
import com.znsb.msfq.utils.LogUtils;
import com.znsb.msfq.utils.NetworkAddress;
import com.znsb.msfq.utils.OverallData;
import com.znsb.msfq.utils.ResponseUtils;
import com.znsb.msfq.utils.SPUtils;
import com.znsb.msfq.utils.StringUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 孙科技 on 2017/2/14.
 */

public class ShowBillDetailActvity extends BaseActivity {
    @Bind(R.id.bar_img_back)
    ImageView barImgBack;
    @Bind(R.id.bar_tv_title)
    TextView barTvTitle;
    @Bind(R.id.bar_tv_other)
    TextView barTvOther;
    @Bind(R.id.detail_img_head)
    ImageView detailImgHead;
    @Bind(R.id.detail_tv_name)
    TextView detailTvName;
    @Bind(R.id.detail_tv_time)
    TextView detailTvTime;
    @Bind(R.id.detail_tv_shopname)
    TextView detailTvShopname;
    @Bind(R.id.addbill_ratingbar1)
    RatingBar addbillRatingbar1;
    @Bind(R.id.addbill_ratingbar2)
    RatingBar addbillRatingbar2;
    @Bind(R.id.addbill_ratingbar3)
    RatingBar addbillRatingbar3;
    @Bind(R.id.detail_tv_appraise)
    TextView detailTvAppraise;
    @Bind(R.id.detail_img_shop)
    ImageView detailImgShop;
    @Bind(R.id.detail_img_shop1)
    ImageView detailImgShop1;
    @Bind(R.id.detail_img_shop2)
    ImageView detailImgShop2;

    private String userName;//用户名
    private String information;//晒单信息
    private String createTime;//晒单时间
    private String goodsTitle;//商品名称
    private ArrayList<String> images;//晒单图片，图片对象
    private int qualitySatisfaction;//商品质量满意度，1-5星
    private int speedSatisfaction;//发货速度满意度，1-5星
    private int serviceSatisfaction;//服务态度满意度，1-5星

    private Handler hander = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 300:
                    SetData();
                    break;
            }

        }

    };

    @Override
    protected int getlayoutView() {
        return R.layout.act_showbill_detail;
    }

    @Override
    protected void initView(View view) {
        barTvTitle.setText("晒单详情");
    }

    @Override
    protected void initData() {
        images=new ArrayList<>();
        String id=getIntent().getStringExtra("ids");
        String type=getIntent().getStringExtra("type");
        GetCommentsDetail(type,id);
    }

    void SetData() {
        detailTvName.setText(StringUtils.Phone(userName));
        detailTvTime.setText(createTime);
        detailTvTime.setText(GetIsparityTime.InputTimetampAll(createTime));
        detailTvShopname.setText(goodsTitle);
        detailTvAppraise.setText(information);

        addbillRatingbar1.setRating((float) qualitySatisfaction);
        addbillRatingbar2.setRating((float) speedSatisfaction);
        addbillRatingbar3.setRating((float) serviceSatisfaction);
        if (images.size()>0){
            if (images.size()==1){
                ImageLoader.imageLoderNo(detailImgShop,images.get(0));
                detailImgShop.setVisibility(View.VISIBLE);
            }else if (images.size()==2){
                ImageLoader.imageLoderNo(detailImgShop,images.get(0));
                ImageLoader.imageLoderNo(detailImgShop1,images.get(1));
                detailImgShop.setVisibility(View.VISIBLE);
                detailImgShop1.setVisibility(View.VISIBLE);

            }else if (images.size()>2){
                ImageLoader.imageLoderNo(detailImgShop,images.get(0));
                ImageLoader.imageLoderNo(detailImgShop1,images.get(1));
                ImageLoader.imageLoderNo(detailImgShop2,images.get(2));
                detailImgShop.setVisibility(View.VISIBLE);
                detailImgShop1.setVisibility(View.VISIBLE);
                detailImgShop2.setVisibility(View.VISIBLE);

            }
        }
    }

    //公共晒单列表
    public void GetCommentsDetail(final String type,String id) {

        Map<String, Object> maps = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        String url="";
        try {
            maps.put("id", id);//晒单ID
            if (type.equals("0")){
                url=NetworkAddress.CommentsDetails;
                map=HttpUtils.getLoginMaps(maps);
            }else {
                url=NetworkAddress.CommentsDetail;
                map=HttpUtils.getMaps(maps);
            }
            HttpUtils.getInstance(this).postAsnyRequest(url,map , new ResponseUtils() {
                @Override
                public void onSucceed(String code, JSONObject request) {
                    try {
                        LogUtils.i("", ">>>>>>GetCommentsDetail" + request);
                        userName = JsonResult.getString(request, "userName");
                        createTime = JsonResult.getString(request, "createTime");
                        goodsTitle = JsonResult.getString(request, "goodsTitle");
                        information = JsonResult.getString(request, "information");
                        qualitySatisfaction = JsonResult.getInt(request, "qualitySatisfaction");
                        speedSatisfaction = JsonResult.getInt(request, "speedSatisfaction");
                        serviceSatisfaction = JsonResult.getInt(request, "serviceSatisfaction");
                        JSONArray imageslist = JsonResult.getJSONArray(request, "images");
                        for (int i = 0; i < imageslist.length(); i++) {
                            JSONObject object = imageslist.getJSONObject(i);
                            images.add(JsonResult.getString(object, "url"));
                        }
                    } catch (Exception e) {
                        LogUtils.i("", ">>>>>>e" + e);
                    }
                    hander.sendEmptyMessage(300);
                }

                @Override
                public void onFailure(String result) {
                    LogUtils.i("", ">>>>>>onFailure" + result);
                }
            });
        } catch (Exception e) {

        }
    }


    @Override
    protected void clickListener(View view) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.bar_img_back)
    public void onClick() {
        finish();
    }
}
